// pages/mine/mine.js
const app = getApp()
const util = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    articleList: [],
    videoList: [],
    is_video_open: false,
    is_article_open: false,


    musicflag: false,
    videoflag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.getCollection()

    wx.request({
      url: app.globalData.url + 'medias/config?v=' + app.globalData.version,
      success: (res) => {
        that.setData({
          musicflag: res.data.data.musicflag,
          videoflag: res.data.data.videoflag
        })
      }
    });
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getCollection()
  },

  // 获取收藏
  getCollection: function() {
    var that = this

    var videoStorage = wx.getStorageSync('videoStorage')
    if (videoStorage) {
      videoStorage = util.beSet(videoStorage)
      wx.setStorageSync('videoStorage', videoStorage)
      that.setData({
        videoList: videoStorage
      })
    }

    var articleStorage = wx.getStorageSync('articleStorage')
    if (articleStorage) {
      articleStorage = util.beSet(articleStorage)
      wx.setStorageSync('articleStorage', articleStorage)
      that.setData({
        articleList: articleStorage
      })

    }
  },

  //
  openVideoScroll: function() {
    var that = this
    that.setData({
      is_video_open: !that.data.is_video_open
    })
    if (that.data.is_article_open) {
      that.setData({
        is_article_open: !that.data.is_article_open
      })
    }

  },
  openArticleScroll: function() {
    var that = this
    that.setData({
      is_article_open: !that.data.is_article_open
    })
    if (that.data.is_video_open) {
      that.setData({
        is_video_open: !that.data.is_video_open
      })
    }
  },

  // 点击播放视频
  playVideo: function(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/detail/detail?type=shareVideo&id=' + id,
      success: (res) => {}
    });
  },

  // 打开文章
  openArticle: function(e) {
    var id = e.currentTarget.dataset.id
    wx.setStorageSync('artId', id)
    wx.switchTab({
      url: '/pages/index/index',
      success: (res) => {}
    });
  },


  // 删除收藏视频
  delete:function(e){
    wx.showLoading();
    var id = e.currentTarget.dataset.id
    var storage = wx.getStorageSync("videoStorage");
    for(let i = 0 ; i < storage.length ; i ++){
      if(storage[i].id == id){
        storage.splice(i , 1);
      }
    }
    wx.setStorageSync('videoStorage', storage)
    this.setData({
      videoList:storage
    })
    wx.hideLoading();
  },

  // 删除收藏文章
  deleteArticle:function(e){
    wx.showLoading();
    var id = e.currentTarget.dataset.id
    var storage = wx.getStorageSync("articleStorage");
    for(let i = 0 ; i < storage.length ; i ++){
      if(storage[i].id == id){
        storage.splice(i , 1);
      }
    }
    wx.setStorageSync('articleStorage', storage)
    this.setData({
      articleList:storage
    })
    wx.hideLoading();
  },

  /**
   * 跳转盒子
   */
  tohezi:function(){
    wx.navigateToMiniProgram({
      appId:'wx7c0beb2fc324a64e',
      success: (res)=>{}
    });
  }
})